<?php

namespace app\admin\controller\mobile;

use Exception;
use think\App;
use app\Request;
use think\facade\Log;
use think\response\Json;

use app\admin\model\SystemQuick;
use app\admin\model\WorkAttendance;
use app\admin\model\WorkAttendanceStatistics;
use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnnotation;

#[ControllerAnnotation(title: '移动端管理')]
class Index extends AdminController
{

    private array $notes;

    protected bool $ignoreLogin = true;

    #[NodeAnnotation(ignore: ['add', 'delete', 'edit', 'export', 'modify', 'recycle'])]
    protected array $ignoreNode;

    private $token = "d2f066d6d5a12cd217a0cbedc5f6d90b";

    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    #[NodeAnnotation(title: '列表', auth: true)]
    public function index(Request $request): Json|string
    {
        $month = date("Y-m");
        $msg = $month . " 出勤";
        //星期
        $week = $this->week();

        //万年历
        $wnl = $this->wnl();
        $warningDay = false;
        $class = "layui-card-body";
        if (!isset($wnl['code'])) {
            $gzDate = $wnl["gzDate"];
            $array = ["寅", '巳', '申', '亥'];
            for ($i = 0; $i < count($array); $i++) {
                $day = $array[$i];
                if (strpos($gzDate, $day) !== false) {
                    $warningDay = true;
                }
            }
            if ($warningDay) {
                $class = "layui-card-body layui-font-red";
            }
        } else {
            $wnl = null;
        }

        //查询天气
        $today = "";
        $tomorrow = "";
        $weather = $this->weather();
        if ($weather['code'] != 0) {
            $data = $weather['data']["data"];
            $today = $this->setupWeather(array_values($data)[1]);
            $tomorrow = $this->setupWeather(array_values($data)[2]);
        }

        //本月出勤统计
        $month = date("Y-m");
        $model = WorkAttendanceStatistics::where("statistics_date", $month . '-01')->findOrEmpty();
        if ($model->isEmpty()) {
            $model = new WorkAttendanceStatistics();
        }
        $today_attendance = today_attendance();
        $this->assign(compact('msg', 'model', 'week', 'wnl', 'class', 'today', 'tomorrow', 'today_attendance'));
        return $this->fetch();
    }

    #[NodeAnnotation(title: '出勤管理', auth: true)]
    public function attendance(Request $request): Json|string
    {

        return $this->fetch('attendance-index');
    }

    ///获取星期
    public function week()
    {
        $weekArray = ["日", "一", "二", "三", "四", "五", "六"];
        return $weekArray[date("w", time())];
    }

    ///万年历
    public function wnl()
    {
        try {
            $url = 'https://api.52vmy.cn/api/wl/wnl';
            $client = new \GuzzleHttp\Client();
            $response = $client->request('GET', $url);
            return json_decode($response->getBody(), true);
        } catch (Exception $e) {
            $back = ['code' => 0];
            return $back;
        }
    }

    ///天气
    public function weather()
    {
        try {
            $url = 'https://apii.52vmy.cn/api/query/tian/three?city=滨城区&token=' . $this->token;
            $client = new \GuzzleHttp\Client();
            $response = $client->request('GET', $url);
            return json_decode($response->getBody(), true);
        } catch (Exception $e) {
            $back = ['code' => 0, 'data' => ['city' => '', 'data' => []]];
            return $back;
        }
    }

    public function setupWeather($item)
    {
        return $item["Time"] . " " . $item["weather"] . " " . $item["temperature"];
    }

    /**
     * 出勤统计
     */
    public function attendanceStatistics()
    {
        $data = attendanceStatistics();
        return json($data);
    }
}
